package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.utils.net.Uri;

public class MessagingIntents {
    private Context context;
    private Intent intent;

    private MessagingIntents(Context context)
    {
        this.context = context;
    }

    public static MessagingIntents from(Context context)
    {
        return new MessagingIntents(context);
    }

    public MessagingIntents openMessages()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addEntity("android.intent.category.APP_MESSAGING");
        intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        return this;
    }

    public MessagingIntents createEmptySms()
    {
        return createSms(null, (String[]) null);
    }

    public MessagingIntents createEmptySms(String phoneNumber)
    {
        return createSms(null, new String[]{phoneNumber});
    }

    public MessagingIntents createEmptySms(String[] phoneNumbers)
    {
        return createSms(null, phoneNumbers);
    }

    public MessagingIntents createSms(String body)
    {
        return createSms(body, (String[]) null);
    }

    public MessagingIntents createSms(String body, String phoneNumber)
    {
        return createSms(body, new String[]{phoneNumber});
    }

    public MessagingIntents createSms(String body, String[] phoneNumbers)
    {
        Uri smsUri;
        if (phoneNumbers == null || phoneNumbers.length == 0)
        {
            smsUri = Uri.parse("smsto:");
        } else
        {
            smsUri = Uri.parse("smsto:" + Uri.encode(String.join(",", phoneNumbers)));
        }

        intent = new Intent();
        intent.setAction("android.intent.action.SENDTO");
        intent.setUri(smsUri);
        //Telephony.Sms.getDefaultSmsPackage API need to bo found
       // intent.setBundle(Telephony.Sms.getDefaultSmsPackage(context));


        if (body != null)
        {
            intent.setParam("sms_body", body);
        }

        return this;
    }

    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
